﻿using MB.RuleBase.Common;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class BLocationReplenish
    {

        //拣货货位信息
        public List<LocationGoodsInfoForPlenish> GetPickLocationGoodsInfo(string GoodsCode)
        {
            List<LocationGoodsInfoForPlenish> list = null;
            try
            {
                list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LocationGoodsInfoForPlenish>("PickReplenish", "GetPickLocationGoodsByGoodCode", GoodsCode);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
            return list;

        }
        #region 分拣货位补货
        public List<LocationGoodsInfoForPlenish> GetPickCellNo()
        {
            try
            {
                List<LocationGoodsInfoForPlenish> PickCellNo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LocationGoodsInfoForPlenish>("PickReplenish", "GetPickCellNo", "");
                if (PickCellNo.Count == 0)
                {
                    PickCellNo = null;
                }
                return PickCellNo;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public List<PickReplenishInfo> GetPickLocationGoods(string batchNo, DateTime rationDate)
        {
            try
            {
                List<PickReplenishInfo> PickInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PickReplenishInfo>("PickReplenish", "GetPickLocationGoodsInfo", batchNo, rationDate);
                if (PickInfo.Count == 0)
                {
                    PickInfo = null;
                }
                return PickInfo;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public bool UpdateReplenishStatus(string userID, List<PickReplenishInfo> lstPickReplenishInfo)
        {
            bool outPut = false;


            //定义事物
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;

            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("PickReplenish", "UpdateReplenishStatus", lstPickReplenishInfo);
                }
                outPut = true;
                tran.Commit();
                return outPut;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                outPut = false;
                return outPut;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }
        #endregion


        public List<PickReplenishInfo> PickTaskQuery(string locationCode, string batchNo, string rationDate)
        {
            try
            {
                List<PickReplenishInfo> OrderInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PickReplenishInfo>("Pick", "GetPickTaskSituation", batchNo, rationDate, locationCode);

                return OrderInfo;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }

        }
    }
}
